package sk.patt.eshop.pages.admin;

import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.HeadersToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.NavigationToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import sk.patt.eshop.CategoriesProvider;
import sk.patt.eshop.data.dao.interfaces.BrandDao;
import sk.patt.eshop.data.dao.interfaces.CategoryDao;
import sk.patt.eshop.data.dataobjects.Brand;
import sk.patt.eshop.data.dataobjects.Category;

public class AdminCategoriesPage extends AdminPage {

	@SpringBean
	private CategoryDao categoryDao;
		
	private TextField<String> categoryTitle;
	private CategoriesProvider categoriesProvider = new CategoriesProvider(categoryDao);
	
	public AdminCategoriesPage() {
		IColumn[] columns = new IColumn[2];
		columns[0] = new PropertyColumn(new Model<String>("Category ID"), "id", "id");
		 columns[1] = new PropertyColumn(new Model<String>("Category name"), "title", "title");
		 
		 DataTable table = new DataTable("datatable", columns, categoriesProvider, 10);
		 
		 table.addBottomToolbar(new NavigationToolbar(table));
		 table.addTopToolbar(new HeadersToolbar(table, categoriesProvider));
		 add(table);
		 
		categoryTitle = new TextField<String>("categoryTitle", Model.of(""));
		categoryTitle.setRequired(true);
		
		Form<Category> brandForm = new Form<Category>("insertCategoryForm"){
			@Override
			protected void onSubmit() {
				Category newCategory = new Category();
				newCategory.setTitle(categoryTitle.getInput());
				categoryDao.save(newCategory);
				List<Category> categoriesss = categoryDao.findAll();
				for (Category c:categoriesss) {
					System.out.println(c.getTitle());
				}
			}
		};
		
		add(brandForm);
		brandForm.add(categoryTitle);
	}
}
